package com.amos.wx.mp.handler;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import com.amos.wx.util.MessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author amos
 */
@Component
public class DefaultMessageHandlerFactory implements MessageHandlerFactory {
    private final static Logger logger = LoggerFactory.getLogger(MessageUtils.class);
    private final Map<String, MessageHandler> messageHandlerMap = new ConcurrentHashMap<>();
    @Override
    public void register(String name, MessageHandler messageHandler) {
        messageHandlerMap.put(name, messageHandler);
    }
    @Override
    public MessageHandler getHandler(String name) {
        logger.info("messageHandlerMap:{}",messageHandlerMap.toString());
        logger.info("接收处理:{}",name);
        MessageHandler messageHandler = messageHandlerMap.get(name);
        if (Objects.isNull(messageHandler)) {
            throw new IllegalArgumentException("Unsupported message, name=[" + name + "]");
        }
        return messageHandler;
    }
}
